import { CommonApi } from "./CommonApi";
import { StockInfo, PriceRes } from "..";
const http = require('http');
let iconv = require("iconv-lite");



export class Sina implements CommonApi{

    
    intervalSearch(items: string[], callback: (res: Map<string, PriceRes>) => void): void {

        var req = http.get(`http://hq.sinajs.cn//list=${items.join(',')}`,(res) => { 
            res.setEncoding('utf8'); 
            res.on('data', function (chunk) { 
                let resObj: Map<string, PriceRes> = new Map<string, PriceRes>();
                let dataArr = chunk.split(';');
                if (dataArr.length > 0) {
                    for (let i=0; i<items.length; i++) {
                        let code = items[i];
                        let data = dataArr[i].replace(/\s+/g,"");
                        if (data !== `var hq_str_${code}=""` && data != '') {
                            data = data.replace(`varhq_str_${code}=`, '').replace('"', '');
                            let detail = data.split(',')
                            resObj[code] = {
                                startPrice: detail[1],
                                currentPrice: detail[3]
                            }
                        }
                    }
                    callback(resObj);
                }
            }); 
        }); 
           
        req.on('error', function (e) { 
            console.log('problem with request: ' + e.message); 
        }); 
    }



    keywordSearch(key: string, callback: (info: StockInfo[]) => void) {
        var req = http.get(`http://suggest3.sinajs.cn/suggest/type=&key=${key}`, (res) => {
            let dataStr = "";
            
            let buffer = [];
            let bufferLength = 0;
            res.on("data", chunk => {
                buffer.push(chunk);
                bufferLength += chunk.length;
            });
            res.on("end", () => {
                let bufferData = Buffer.concat(buffer, bufferLength);
                dataStr = iconv.decode(bufferData, "GBK");
                let data = [];
                if (dataStr !== 'var suggestvalue="";') {
                    let response = dataStr.replace('var suggestvalue="', '').replace('";', '');
                    var arr = response.split(';');
                    let flag = 0;
                    for (var i=0; i<arr.length; i++) {
                        let detail = arr[i].split(',');
                        let single = {
                            sharesName: detail[0],
                            sharesCode: detail[3]
                        }
                        data.push(single);

                        flag ++ ;
                        if (flag > 10) {
                            break;
                        }
                    }
                }
                callback(data);
            });
        });
        req.on('error', function (e) { 
            console.log('problem with request: ' + e.message); 
        }); 
    }


}